package com.clh.controller;

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clh.common.PageInfo;
import com.clh.dto.Result;
import com.clh.entity.Pay;
import com.clh.entity.Repair;
import com.clh.service.RepairService;
import com.clh.service.impl.RepairServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * Servlet implementation class RepairServlet
 */
@WebServlet("/RepairServlet")
public class RepairServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RepairService repairService = new RepairServiceImpl();
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String op = request.getParameter("op");
		
			if("repairPage".equals(op)) {
				doRepairPage(request,response);
			}else if ("repairEdit".equals(op)) {
				doRepairEdit(request,response);
			}else if ("repairUpdate".equals(op)) {
				
				doRepairUpdate(request,response);
			}else if ("repairDelete".equals(op)) {
				doRepairDelete(request,response);
			}else if("ownerRepairList".equals(op)) {
				doOwnerRepair(request,response);
			}else if("ownerRepairStatusUpdate".equals(op)) {
				doOwnerRepairStatusUpdate(request,response);
			}else if("ownerRepairAdd".equals(op)) {
				doOwnerRepairAdd(request,response);
			}else if ("payAdd".equals(op)) {
				doPayAdd(request,response);
			}
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	//用户缴费单号添加
	private void doPayAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		String repairNo = request.getParameter("repairNo");
		String roomNo = request.getParameter("roomNo");
		String payMoney = request.getParameter("payMoney");
		
		Pay pay = new Pay();
		pay.setPayMoney(new BigDecimal(payMoney));
		pay.setRoomNo(roomNo);
		
		boolean f = repairService.ownerRepairPay(pay, repairNo);
		if(f) {
			result.ok(f).setMessage("成功生成！");
		}else {
			result.fail(0, "生成失败！");
		}
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doOwnerRepairAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		
		String roomNo = request.getParameter("roomNo");
		String repairContent = request.getParameter("repairContent");
		Repair repair = new Repair();
		repair.setRepairContent(repairContent);
		repair.setRoomNo(roomNo);
		
		boolean f = repairService.ownerRepairInsert(repair);
		
		if(f) {
			result.ok(f);
		}else {
			result.fail(0, "插入失败");
		}
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doOwnerRepairStatusUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		String repairNo = request.getParameter("repairNo");
		String repairStatus = request.getParameter("repairStatus");
		
		boolean f = repairService.ownerRepairStatusUpdate(repairNo, repairStatus);
		if(f) {
			result.ok(f);
		}else {
			result.fail(0, "状态修改失败");
		}
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doOwnerRepair(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		String phoneNumber = request.getParameter("phoneNumber");
		String repairContent =request.getParameter("repairContent");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		

		if(pageNum == null || "".equals(pageNum) || Integer.parseInt(pageNum)<0
				|| pageSize == null || "".equals(pageSize) || Integer.parseInt(pageSize)<0) {
			result.fail(0, "参数错误");
			
		}else {
			PageInfo<Repair> pageInfo = repairService.ownerShowRepairList(phoneNumber, repairContent, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
			if(!pageInfo.getList().isEmpty()) {
				result.ok(pageInfo);
			}else {
				result.fail(0, "没有相关数据");
			}
			
			response.setContentType("application/json;charset=utf-8");
			ObjectMapper objectMapper = new ObjectMapper();
			String json = objectMapper.writeValueAsString(result);
			response.getWriter().write(json);
			
		}
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	//报修信息修改
	private void doRepairDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		
		String repairNo = request.getParameter("repairNo");
		boolean flag = repairService.deleteRepairInformation(repairNo);
		if(flag) {
			result.ok(flag).setMessage("删除成功");
		}else {
			result.fail(0, "删除失败");
		}
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
	}

	/**
	 * @param request
	 * @param response
	 *修改保修信息
	 * @throws IOException 
	 */
	private void doRepairUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		String repairHandler = request.getParameter("repairHandler");
		String repairStatus = request.getParameter("repairStatus");
		String repairNo = request.getParameter("repairNo");
		String repairDealTime = request.getParameter("repairDealTime");
		
		boolean flag = repairService.updateRepairInformation(repairHandler, repairStatus, repairNo, repairDealTime);
		if(flag) {
			result.ok(flag).setMessage("修改成功");
		}else {
			result.fail(0, "修改失败");
		}
		
		
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		
		response.getWriter().write(json);
		
		
	}

	/**
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doRepairEdit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		String repairNo = request.getParameter("repairNo");
		

		if(repairNo == null || "".equals(repairNo)) {
			result.fail(0, "参数错误");
		}else {
			Repair repair = repairService.findByIdRepairInformation(repairNo);
			if(repair != null) {
				result.ok(repair);
			}else {
				result.fail(0, "没有相关数据");
			}
		}
		
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
		
	}

	/**
	 * @param request
	 * @param response
	 * 报修列表的分页查询
	 * @throws IOException 
	 */
	private void doRepairPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Result result = new Result();
		//获取pageNum pageSize
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		String repairNo = request.getParameter("repairNo");
		String repaired = request.getParameter("repaired");
		
		
		
			if(pageNum == null || "".equals(pageNum) || Integer.parseInt(pageNum)<0
					|| pageSize == null || "".equals(pageSize) || Integer.parseInt(pageSize)<0) {
				result.fail(0, "参数错误");
				
			}else {
				Repair repair = new Repair();
				
				
				repair.setRepairCreateTime(start);
				repair.setRepairDealTime(end);
				repair.setRepairNO(repairNo);
				PageInfo<Repair> pageInfo = repairService.showRepairList(repair, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
				if(!pageInfo.getList().isEmpty()) {
					result.ok(pageInfo);
				}else {
					result.fail(0, "没有相关数据");
				}
			}
	
		
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(result);
		response.getWriter().write(json);
		
		
	}

}
